import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
import {getToken} from "../plugins/cookie";
// import.meta.env.VITE_APP_URL as any

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        redirect: "/login"
    },
    {
        path: "/login",
        name: "Login",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/login/index.vue"),
    },
    {
        path: "/register",
        name: "Register",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/register/index.vue"),
    },
    {
        path: "/setPassWord",
        name: "SetPassWord",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/setPassWord/index.vue"),
    },
    {
        path: "/home",
        name: "Home",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/home/index.vue"),
    },
    {
        path: "/user",
        name: "User",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/user/index.vue"),
    },
    {
        path: "/recordView",
        name: "RecordView",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/recordView/index.vue"),
    },
    {
        path: "/recordDetail",
        name: "RecordDetail",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import("../view/recordView/detail.vue"),
    },
];
// import.meta.env.VITE_APP_URL as any
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
});

router.beforeEach((to, from, next) => {

    let token = getToken();//这里根据自己项目的实际情况添加是否登录的判断条件
    // @ts-ignore
    if ((to.name !== 'Login' && !token) && to.name !== 'SetPassWord' && to.name !== 'Register') {
        console.log("123")
        next({name: 'Login'})
    } else next()
})


export default router;

